package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.tanhua.dubbo.api.mongo.SimilarYouApi;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.Report;
import com.tanhua.model.mongo.UserLike;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
@Slf4j
public class SimilarYouApiImpl implements SimilarYouApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Report> findReportUserIds(Long userId, List<Long> excludeIds) {
        //1.根据当前id获取报告分数
        Query queryid = new Query();
        queryid.addCriteria(Criteria.where("userId").is(userId));
        Report currentUserReport = mongoTemplate.findOne(queryid, Report.class);

        //2.根据当前id的报告分数查询相似者的用户信息
        //2.1 设定相似者分数的范围,并查询相似者的报告
        Double low_score = currentUserReport.getScore() - 7 < 0 ?  0.0 : currentUserReport.getScore() - 7;
        Double high_score = currentUserReport.getScore() + 7 > 100 ? 100.0 : currentUserReport.getScore() + 7;
        Query queryscore = new Query();
        //queryscore.addCriteria(Criteria.where("score").gte(low_score).lte(high_score).and("userId").nin(userId)); //查询相似者报告并排除自己
        queryscore.addCriteria(Criteria.where("score").gte(low_score).lte(high_score));

        //判断相似者是否为空
        if (CollectionUtils.isNotEmpty(excludeIds)){
            excludeIds.add(userId);
            queryscore.addCriteria(Criteria.where("userId").nin(excludeIds));
        }else {
            queryscore.addCriteria(Criteria.where("userId").nin(userId));
        }

        List<Report> reports = mongoTemplate.find(queryscore, Report.class);
        return reports;
    }

}
